package com.cdh.edu.controller.admin;


import com.cdh.edu.entity.Chapter;
import com.cdh.edu.entity.vo.ChapterVo;
import com.cdh.edu.service.ChapterService;
import com.cdh.utils.result.Restful;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cdh
 * @since 2020-10-20
 */
@ApiModel(description = "章节管理")
@RestController
@RequestMapping("chapter")
@CrossOrigin
public class ChapterController {
    @Autowired
    private ChapterService chapterService;

    @ApiModelProperty("课程大纲列表,根据课程ID进行查询")
    @GetMapping("getChapterVideo/{courseId}")
    public Restful getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);
        return Restful.ok().data("allChapterVideo",list);
    }

    @ApiModelProperty("添加章节")
    @PostMapping("addChapter")
    public Restful addChapter(@RequestBody Chapter chapter){
        chapterService.save(chapter);
        return Restful.ok();
    }

    @ApiModelProperty("根据章节id查询")
    @GetMapping("queryByChapterId/{chapterId}")
    public Restful queryByChapterId(@PathVariable String chapterId){
        Chapter chapter = chapterService.getById(chapterId);
        return Restful.ok().data("chapter",chapter);
    }

    @ApiModelProperty("修改章节")
    @PostMapping("updateChapter")
    public Restful updateChapter(@RequestBody Chapter chapter){
        chapterService.updateById(chapter);
        return Restful.ok();
    }

    @ApiModelProperty("根据ID删除章节")
    @DeleteMapping("{chapterId}")
    public Restful deleteByChapterId(@PathVariable("chapterId") String chapterId){
        //TODO:删除课程视频 调用VOD中的删除视频文件的接口

        boolean isOK = chapterService.deleteChapterById(chapterId);
        if (isOK){
            return Restful.ok();
        }else{
            return Restful.error();
        }
    }
}

